package com.huhu.service;

import com.huhu.dao.RemoteDao;
import com.huhu.dao.RoleDao;
import com.huhu.dao.UserDao;
import com.huhu.entity.Remote;
import com.huhu.entity.Role;
import com.huhu.entity.User;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Set;

/**
 * @TODO Created by xuyi on 2016/9/6.
 */
@Service
public class UserService {

    @Autowired
    private RemoteDao remoteDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;

    public void initUser(String userName) {

        User user = userDao.findByName(userName);
        SecurityUtils.getSubject().getSession().setAttribute("userId", user.getUserId());
    }

    public int insertRemote(Remote remote) {
        return remoteDao.insertRemote(remote);
    }

    public User findURoleAndPer(User user) {
//        List<Role> roleList = roleDao.findURoleList(user.getUserId());
        Set<String> allRoleSet = roleDao.findAllRole(user.getUserId());
        Set<String> allPerSet = roleDao.findAllPermission(user.getUserId());

        user.setAllRole(allRoleSet);
        user.setAllPermission(allPerSet);
        return user;
    }

    public User findByName(String userName) {

        return userDao.findByName(userName);
    }

    public int register(User user) {

        return userDao.register(user);
    }

}
